ewa = {

	setCookie:function (ck_key,ck_value,exdays){
	    var d = new Date();
	    d.setTime(d.getTime()+(exdays*24*60*60*1000));
	    var expires = "expires="+d.toGMTString();
	    document.cookie = ck_key+"="+ck_value+"; "+expires+"; path=/";
	},

	getCookie:function (ck_key){
	    var name = ck_key + "=";
	    var ca = document.cookie.split(';');
	    for(var i=0; i<ca.length; i++) {
	        var c = ca[i].trim();
	        if (c.indexOf(name)==0) { return c.substring(name.length,c.length); }
	    }
	    return "";
	},

	getPage:function (target_url){
	    window.location.href = target_url;
	},
	
	getPageWithToken:function (Username, Token, target_url){
		ewa.setCookie("username", Username, 1); ewa.setCookie("token", Token, 1);
	    window.location.href = target_url;
	},

	ApiLogin:function (IdUsername, IdPassword, ApiPath, TargetPagePath){
	    var Username = document.getElementById(IdUsername).value;
	    var Password = document.getElementById(IdPassword).value;
	    var Params = {"Username": Username, "Password": Password};
	    // alert("Username:"+ Username +", Password:" + Password)
	    $.post(ApiPath, Params, function(ReplyData, ResponseStatus){
	        ewa.getPageWithToken(Username, ReplyData.token, TargetPagePath)
	　　},"json");
	},

	ApiAddUser:function (IdInput1, IdInput2, ApiPath) {
	    var Input1 = document.getElementById(IdInput1).value;
	    var Input2 = document.getElementById(IdInput2).value;
	    var Params = {"user": Input1, "pass": Input2};
    	$.ajax({
	        type: "POST",
	        url: ApiPath,
	        data: Params,
	        beforeSend: function(xhr) {
	          xhr.setRequestHeader('Authorization', 'Bearer ' + ewa.getCookie("token") );
	        },
	        success: function(EchoReplyMessage) {
	          //alert(EchoReplyMessage)
	        }        
    	});
	},

	ApiChangePass:function (IdInput1, IdInput2, IdInput3, ApiPath) {
	    var Input1 = document.getElementById(IdInput1).value;
	    var Input2 = document.getElementById(IdInput2).value;
	    var Input3 = document.getElementById(IdInput3).value;
	    var Params = {"user": Input1, "pass": Input2, "newpass": Input3};
    	$.ajax({
	        type: "POST",
	        url: ApiPath,
	        data: Params,
	        beforeSend: function(xhr) {
	          xhr.setRequestHeader('Authorization', 'Bearer ' + ewa.getCookie("token") );
	        },
	        success: function(EchoReplyMessage) {
	          //alert(EchoReplyMessage)
	        }        
    	});
	},

	//////////////////////  User functions  /////////////////////////////
	api0:function (IdInput1, IdInput2, ApiPath) {
	    var Input1 = document.getElementById(IdInput1).value;
	    var Input2 = document.getElementById(IdInput2).value;
	    var Params = {"data1": Input1, "data2": Input2};
    	$.ajax({
	        type: "POST",
	        url: ApiPath,
	        data: Params,
	        beforeSend: function(xhr) {
	          xhr.setRequestHeader('Authorization', 'Bearer ' + ewa.getCookie("token") );
	        },
	        success: function(EchoReplyMessage) {
	          //alert(EchoReplyMessage)
	        }        
    	});
	}
};